package com.zzt.java_doc_search.tests;

import com.zzt.java_doc_search.common.Utils;
import org.openqa.selenium.By;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description
 * User: panda
 * Date: 2025-07-08
 * Time: 18:06
 */
public class IndexPage extends Utils {
    public static String url = "http://123.56.184.183:8080/index.html";

    public IndexPage() {
        super(url);
    }

    /**
     * 页面可以正常打开
     */
    public void checkPageRight() throws IOException {
        // 检查输入框
        driver.findElement(By.cssSelector("body > div > div.header > input[type=text]"));
        String btnText = driver.findElement(By.cssSelector("#search-btn")).getText();
        assert "搜索".equals(btnText);
        screenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
    }

    /**
     * 验证搜索框：输入内容为空
     */
    public void checkSearchEmptyResult() throws IOException {
        driver.findElement(By.cssSelector("body > div > div.header > input[type=text]")).clear();
        driver.findElement(By.cssSelector("#search-btn")).click();
        String resultCount = driver.findElement(By.cssSelector("body > div > div.result > div")).getText();
        assert "当前找到 0个结果！".equals(resultCount);
        screenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
    }

    /**
     * 验证搜索框：输入内容为空格
     */
    public void checkSearchSpaceResult() throws IOException {
        // 一个空格
        driver.findElement(By.cssSelector("body > div > div.header > input[type=text]")).clear();
        driver.findElement(By.cssSelector("body > div > div.header > input[type=text]")).sendKeys(" ");
        driver.findElement(By.cssSelector("#search-btn")).click();
        String resultCount = driver.findElement(By.cssSelector("body > div > div.result > div")).getText();
        assert "当前找到 0个结果！".equals(resultCount);
        screenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
        // 两个空格
        driver.findElement(By.cssSelector("body > div > div.header > input[type=text]")).clear();
        driver.findElement(By.cssSelector("body > div > div.header > input[type=text]")).sendKeys("  ");
        driver.findElement(By.cssSelector("#search-btn")).click();
        resultCount = driver.findElement(By.cssSelector("body > div > div.result > div")).getText();
        assert "当前找到 0个结果！".equals(resultCount);
        screenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
    }

    /**
     * 验证搜索框：输入内容为非关键词
     */
    public void checkSearchResultNotFind() throws IOException {
        driver.findElement(By.cssSelector("body > div > div.header > input[type=text]")).clear();
        driver.findElement(By.cssSelector("body > div > div.header > input[type=text]")).sendKeys("aaaa");
        driver.findElement(By.cssSelector("#search-btn")).click();
        String resultCount = driver.findElement(By.cssSelector("body > div > div.result > div")).getText();
        assert "当前找到 0个结果！".equals(resultCount);
        screenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
    }

    /**
     * 验证搜索框：输入内容为关键词
     */
    public void checkSearchResultFind() throws IOException {
        driver.findElement(By.cssSelector("body > div > div.header > input[type=text]")).clear();
        driver.findElement(By.cssSelector("body > div > div.header > input[type=text]")).sendKeys("arraylist");
        driver.findElement(By.cssSelector("#search-btn")).click();
        driver.findElement(By.cssSelector("body > div > div.result > div"));
        String key = driver.findElement(By.cssSelector("body > div > div.result > div:nth-child(2) > div.desc > i")).getText();
        assert "arraylist".equals(key);
        screenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
    }

    /**
     * 验证搜索框：输入内容为多个关键词，用空格分开
     */
    public void checkSearchMultipleResultFind() throws IOException {
        driver.findElement(By.cssSelector("body > div > div.header > input[type=text]")).clear();
        driver.findElement(By.cssSelector("body > div > div.header > input[type=text]")).sendKeys("array list");
        driver.findElement(By.cssSelector("#search-btn")).click();
        driver.findElement(By.cssSelector("body > div > div.result > div"));
        String key = driver.findElement(By.cssSelector("body > div > div.result > div:nth-child(2) > div.desc > i")).getText();
        assert "array".equals(key) || "list".equals(key);
        screenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
    }

    /**
     * 验证搜索框：两次输入内容，结果是否一样
     */
    public void checkSearchMultipleResultSame() throws IOException {
        driver.findElement(By.cssSelector("body > div > div.header > input[type=text]")).clear();
        driver.findElement(By.cssSelector("body > div > div.header > input[type=text]")).sendKeys("array list");
        driver.findElement(By.cssSelector("#search-btn")).click();
        String resultCount1 = driver.findElement(By.cssSelector("body > div > div.result > div")).getText();
        String key1 = driver.findElement(By.cssSelector("body > div > div.result > div:nth-child(2) > div.desc > i")).getText();

        driver.findElement(By.cssSelector("body > div > div.header > input[type=text]")).clear();
        driver.findElement(By.cssSelector("body > div > div.header > input[type=text]")).sendKeys("array list");
        driver.findElement(By.cssSelector("#search-btn")).click();
        String resultCount2 = driver.findElement(By.cssSelector("body > div > div.result > div")).getText();
        String key2 = driver.findElement(By.cssSelector("body > div > div.result > div:nth-child(2) > div.desc > i")).getText();
        assert key2.equals(key1) && resultCount1.equals(resultCount2);
        screenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
    }

    /**
     * 验证搜索框：输入正确关键词内容，内容区域是否正常显示
     */
    public void checkContentRight() throws IOException {
        driver.findElement(By.cssSelector("body > div > div.header > input[type=text]")).clear();
        driver.findElement(By.cssSelector("body > div > div.header > input[type=text]")).sendKeys("array list");
        driver.findElement(By.cssSelector("#search-btn")).click();
        driver.findElement(By.cssSelector("body > div > div.result > div"));
        String title = driver.findElement(By.cssSelector("body > div > div.result > div:nth-child(2) > a")).getText();
        String desc = driver.findElement(By.cssSelector("body > div > div.result > div:nth-child(2) > div.desc")).getText();
        String url = driver.findElement(By.cssSelector("body > div > div.result > div:nth-child(2) > div.url")).getText();
        assert !title.isEmpty();
        assert !desc.isEmpty();
        assert !url.isEmpty();
        // 点击标题进行跳转
        driver.findElement(By.cssSelector("body > div > div.result > div:nth-child(2) > a")).click();
        // 切换页面
        for (String windowHandle : driver.getWindowHandles()) {
            if (!windowHandle.equals(driver.getWindowHandle())) {
                driver.switchTo().window(windowHandle);
            }
        }
        assert driver.getCurrentUrl().equals(url);
        screenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
    }
}
